//
//  MineViewController.m
//  TheRoadAD
//
//  Created by baixue on 16/8/4.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "MineViewController.h"
#import "MineHeaderCell.h"
#import "SettingsTableViewCell.h"
#import "LoginViewController.h"//登录
#import "ChangePasswordView.h"//修改密码
#import "FocusOnTableViewController.h"//关注
#import "PersonalHomeViewController.h"//我的
#import "MessageViewController.h"//消息
#import "InformationViewController.h"//个人信息

#define TITLE @[@"我的",@"关注",@"修改密码"]
#define ICONImage @[@"myicon1",@"myicon2",@"myicon3"]

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIView *_headView;//tableView的header
    
}
@property (nonatomic, strong) UITableView *tableView;


@end

@implementation MineViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
//    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self setNavItem];
   
}
//设置导航按钮
-(void)setNavItem{
    
    //左按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 33, 40, 15)];
    leftButton.titleLabel.font = [UIFont fontWithName:@"qingyuan" size:14.0f];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftButton setTitle:@"登录" forState:UIControlStateNormal];
    leftButton.tag = 101;
    [self.view addSubview:leftButton];
    [leftButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    //右按钮
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(Screen_Width-15-20, 30, 15, 18)];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"messageicon_1"] forState:UIControlStateNormal];
    [self.view addSubview:rightButton];
    rightButton.tag = 102;
    [rightButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)back:(UIButton *)btn{
    
    if (btn.tag == 101) {
        // 模拟推出页面，被推出的页面
        LoginViewController *login = [[LoginViewController alloc] init];
       [self presentViewController:login animated:YES completion:nil];

    }else{
        //消息页面
        MessageViewController *message = [[MessageViewController alloc] init];
        [self.navigationController pushViewController:message animated:YES];
        
    }
    
}

//懒加载tableView
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, Screen_Width, Screen_Height) style:UITableViewStyleGrouped];
    }
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;//隐藏滚动条
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    return _tableView;
}

//tableView的代理方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SETTINGSCELL"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SettingsTableViewCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//点击背景色
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   
    cell.setName.text = TITLE[indexPath.row];
    cell.iconImage.image = [UIImage imageNamed:ICONImage[indexPath.row]];
    return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headView = [[UIView alloc] init];
    MineHeaderCell *headerView = [[[NSBundle mainBundle] loadNibNamed:@"MineHeaderCell" owner:nil options:nil] lastObject];
    headerView.frame = CGRectMake(0, 0, Screen_Width, 245 *Screen_Height / 736);
    [headView addSubview:headerView];
    _headView = headerView;
    
    headerView.TOUCHUP = ^(){
        InformationViewController *infor = [[InformationViewController alloc]init];
        [self.navigationController pushViewController:infor animated:YES];
    };
    
    return headView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
         return 245 * Screen_Height / 736;
    }
    return 0.01;
  
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60 * Screen_Height / 736;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    /**
     *  2  修改密码
     *  1  关注
     *  0  我的
     */
    if (indexPath.row == 2) {
        ChangePasswordView *changePWD = [[ChangePasswordView alloc] init];
        [self.navigationController pushViewController:changePWD animated:YES];
    }else if (indexPath.row == 1){
        FocusOnTableViewController *focusOn = [[FocusOnTableViewController alloc]init];
        [self.navigationController pushViewController:focusOn animated:YES];
    }else{
        PersonalHomeViewController *person = [[PersonalHomeViewController alloc] init];
        [self.navigationController pushViewController:person animated:YES];
    }
}

/**
 *  重写这个代理方法就行了，利用contentOffset这个属性改变frame
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    CGFloat offsetY = scrollView.contentOffset.y+20;
    
    if (offsetY < 0) {
        _headView.frame = CGRectMake(offsetY/2, offsetY, Screen_Width - offsetY, 245 * Screen_Height / 736 - offsetY);  // 修改头部的frame值就行了
    }
    /* 往上滑动contentOffset值为正，大多数都是监听这个值来做一些事 */
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
    
}

@end
